import request from "@/utils/request";

export default {
  getById(id) {
    return request({
      url: `/question/getById/${id}`,
      method: "get",
    });
  },
  update(data) {
    const answer = data.answer;
    let arr = answer.sort();
    let str = "";
    for (let index = 0; index < arr.length; index++) {
      const element = arr[index];
      str += element;
      if (index < arr.length - 1) {
        str += "+";
      }
    }
    data.answer = str;
    return request({
      url: `/question/update`,
      method: "post",
      data,
    });
  },

  listBySubject(question, subject, currentPage, pageSize) {
    return request({
      url: `/question/listBySubject`,
      method: "post",
      data: { question, subject, currentPage, pageSize },
    });
  },

  totalBySubject(question, subject) {
    return request({
      url: `/question/totalBySubject`,
      method: "post",
      data: { question, subject },
    });
  },

  list(name, currentPage, pageSize) {
    name = name ? name : "_";
    return request({
      url: `/question/list/${name}/${currentPage}/${pageSize}`,
      method: "get",
    });
  },
  getTotal(name) {
    name = name ? name : "_";
    return request({
      url: `/question/getTotal/${name}`,
      method: "get",
    });
  },
  add(data) {
    let arr = data.answer.sort();
    let answer = "";
    for (let i = 0; i < arr.length; i++) {
      answer += arr[i];
      if (i < arr.length - 1) {
        answer += "+";
      }
    }
    data.answer = answer;
    return request({
      url: `/question/add`,
      method: "post",
      data,
    });
  },
  delete(data) {
    return request({
      url: `/question/delete/${data.id}`,
      method: "delete",
    });
  },
  statisticsBySubjectId(id) {
    return request({
      url: `/question/statisticsBySubjectId/${id}`,
      method: "get",
    });
  },
  statisticsAll() {
    return request({
      url: `/question/statistics/all`,
      method: "get",
    });
  },
};
